<template>
    <div class="header-currency">
        <span @click="openDilaog()">
            <img src="@/assets/images/currency/currency-icon.png" alt="">
            <em v-text="currency"></em>
        </span>

        <UxDialog :visible="dialog" @close="dialog = false">
            <div class="header-currency-dialog">
                <h2>Currency display options</h2>
                <p>Please note that the currency you selected to view on our website will be converted from USD using the current exchange rate. All payments will still be processed in USD.</p>

                <ul>
                    <li :class="{active: active == 'USD'}" @click="active = 'USD'">
                        <img src="@/assets/images/currency/currency-usd.png" alt="">
                        <span>USD (<em>$</em>)</span>
                    </li>
                    <li :class="{active: active == 'AUD'}" @click="active = 'AUD'">
                        <img src="@/assets/images/currency/currency-aud.png" alt="">
                        <span>AUD (<em>$</em>)</span>
                    </li>
                    <li :class="{active: active == 'GBP'}" @click="active = 'GBP'">
                        <img src="@/assets/images/currency/currency-gbp.png" alt="">
                        <span>GBP (<em>£</em>)</span>
                    </li>
                    <li :class="{active: active == 'EUR'}" @click="active = 'EUR'">
                        <img src="@/assets/images/currency/currency-eur.png" alt="">
                        <span>EUR (<em>€</em>)</span>
                    </li>
                    <li :class="{active: active == 'CAD'}" @click="active = 'CAD'">
                        <img src="@/assets/images/currency/currency-cad.png" alt="">
                        <span>CAD (<em>$</em>)</span>
                    </li>
                    <li :class="{active: active == 'HKD'}" @click="active = 'HKD'">
                        <img src="@/assets/images/currency/currency-hkd.png" alt="">
                        <span>HKD (<em>$</em>)</span>
                    </li>
                    <li :class="{active: active == 'JPY'}" @click="active = 'JPY'">
                        <img src="@/assets/images/currency/currency-jpy.png" alt="">
                        <span>JPY (<em>¥</em>)</span>
                    </li>
                    <li :class="{active: active == 'CHF'}" @click="active = 'CHF'">
                        <img src="@/assets/images/currency/currency-chf.png" alt="">
                        <span>CHF (F<em>r</em>)</span>
                    </li>
                    <li :class="{active: active == 'NZD'}" @click="active = 'NZD'">
                        <img src="@/assets/images/currency/currency-nzd.png" alt="">
                        <span>NZD (<em>$</em>)</span>
                    </li>
                </ul>

                <button type="primary" @click="setCurrency(active)">Confirm</button>
                <UxLoading v-if="loading" />

            </div>

        </UxDialog>

    </div>
</template>
<script>
import { mapGetters, mapState } from "vuex";
export default {
    name: "HeaderbarCurrency",
    data () {
        return {
            loading: false,
            dialog: false,
            active: '',
        };
    },
    computed: {
        ...mapState({
            currency: 'currency',
            token: 'token'
        }),
    },

    methods: {
        openDilaog () {
            this.loading = false;
            this.dialog = true;
            this.active = this.currency;
        },
        setCurrency (currency) {
            // this.setCurrency
            // 如果已经登录，则修改用户币种信息
            this.loading = true;

            if (this.token) {
                this.$http('User.bindCurrency/' + currency).then(res => {
                    this.$store.commit('SET_CURRENCY', res.data)
                    this.loading = false;
                    this.dialog = false;
                    this.$uxMessage.success('Currency changed successfully!')
                })
            } else {
                this.$store.dispatch('getCurrency', currency).then(res => {
                    this.loading = false;
                    this.dialog = false;
                    this.$uxMessage.success('Currency changed successfully!')

                })
            }

        },
    },
    mounted () {

    },
};
</script>
<style lang="scss" scoped>
.header-currency {
    & > span {
        height: 26px;
        border-radius: 13px;
        border: 1px solid #e0e0e0;
        cursor: pointer;
        display: flex;
        align-items: center;
        box-sizing: border-box;
        padding: 3px 8px 3px 3px;
        color: #555555;
        transition: all 0.2s;

        img {
            width: 18px;
            height: 18px;
            margin-right: 4px;
        }
        em {
            font-size: 14px;
            // font-family: Roboto-Regular, Roboto;
            font-weight: 400;
            line-height: 16px;
        }
        &:hover {
            background-color: rgba(0, 0, 0, 0.06);
        }
    }
}

.header-currency-dialog {
    display: flex;
    flex-direction: column;
    align-items: center;
    max-width: 448px;
    width: 100%;
    height: 460px;
    display: flex;
    margin: 0 auto;
    position: relative;

    h2 {
        font-size: 18px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;
        color: #000000;
        line-height: 20px;
        margin-top: 42px;
    }

    p {
        margin: 28px 24px 0;
        font-size: 14px;
        font-family: Roboto-Regular, Roboto;
        font-weight: 400;
        color: #555555;
        line-height: 20px;
        text-align: center;
    }

    ul {
        display: flex;
        flex-wrap: wrap;
        padding: 32px 17px 0;
        align-items: center;
        justify-content: center;

        li {
            display: flex;
            align-items: center;
            width: 124px;
            height: 40px;
            margin: 8px 7px;
            box-sizing: border-box;
            padding: 6px 14px;
            background: #ffffff;
            border-radius: 6px;
            border: 1px solid #c6c6c6;
            cursor: pointer;

            img {
                width: 32px;
                height: 28px;
            }
            span {
                margin-left: 12px;
                font-size: 14px;
                font-family: Roboto-Regular, Roboto;
                font-weight: 400;
                color: #000000;
                line-height: 16px;
                white-space: nowrap;
                em {
                    font-family: Arial;
                }
            }

            &.active,
            &:hover {
                border-color: #ea4c89;
            }
        }
    }

    button {
        margin: 32px 0 40px;
        width: 180px;
        height: 38px;
        min-height: 38px;
    }
}
</style>
